cmake_minimum_required(VERSION 3.14)

project(linux-resource-monitor VERSION 1.1.74)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Find Qt
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Widgets Quick)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Widgets Quick)

# 启用自动处理MOC、RCC和UIC文件
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

# 添加所有源文件
set(SOURCES
    src/main.cpp
    src/mainwindow.cpp
    src/cpu_scheduler.cpp
    src/resource_monitor.cpp
)

# 创建可执行文件
add_executable(linux-resource-monitor
    src/main.cpp
    src/mainwindow.cpp
    src/cpu_scheduler.cpp
    src/resource_monitor.cpp
)

# 链接Qt库
target_link_libraries(${PROJECT_NAME} PRIVATE 
    Qt${QT_VERSION_MAJOR}::Core
    Qt${QT_VERSION_MAJOR}::Widgets
    Qt${QT_VERSION_MAJOR}::Quick
)

# 设置安装规则
install(TARGETS ${PROJECT_NAME}
    RUNTIME DESTINATION bin
)

install(DIRECTORY resources/
    DESTINATION share/${PROJECT_NAME}/
)

# Only install desktop and icon files if they exist
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/resources/linux-resource-monitor.desktop")
    install(FILES resources/linux-resource-monitor.desktop DESTINATION share/applications)
else()
    message(WARNING "Desktop file not found: resources/linux-resource-monitor.desktop")
endif()

if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/resources/icons/linux-resource-monitor.png")
    install(FILES resources/icons/linux-resource-monitor.png DESTINATION share/icons/hicolor/48x48/apps)
else()
    message(WARNING "Icon file not found: resources/icons/linux-resource-monitor.png")
endif()

# CPack configuration for DEB packaging
set(CPACK_GENERATOR "DEB")
set(CPACK_PACKAGE_NAME "linux-resource-monitor")
set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH})
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Your Name <your.email@example.com>")
set(CPACK_DEBIAN_PACKAGE_SECTION "utils")
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6, libqt5core5a, libqt5widgets5")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Linux Resource Monitor with CPU scheduler")
set(CPACK_PACKAGE_CONTACT "Your Name <your.email@example.com>")

# 只有当 LICENSE 文件存在时才设置
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
    set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
else()
    message(WARNING "License file not found: LICENSE")
endif()

# 只有当 README.md 文件存在时才设置
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
    set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
endif()

include(CPack)